//
//  BaseViewController.swift
//  ManBetX
//
//  Created by Cocoa on 2020/3/17.
//  Copyright © 2020 Fullen. All rights reserved.
//

/**
* 本类说明:
*
* 用来被 controller 继承的父类，里面做通用配置
*
**/

import UIKit


class BaseViewController: UIViewController,UIGestureRecognizerDelegate{
    
// MARK: -- Property
    
    var _loadView: UIView?
    var isNeedBaseConfigureFlag = true
// MARK: -- function
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if self.isNeedBaseConfigureFlag {
            configureBaseUI()
        }
        
    }
    
    // 配置
    func configureBaseUI() {
        // 左滑返回
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
        // 去掉导航栏下面的一条线
        self.navigationController?.navigationBar.shadowImage = UIImage()
        // 更改导航栏标题字体、字体大小、颜色
        let dict: NSDictionary = [NSAttributedString.Key.foregroundColor: UIColor.init("4B4B4B") ?? UIColor.black, NSAttributedString.Key.font: UIFont(name: "PingFangSC-Regular", size: 18.0)!]
        self.navigationController?.navigationBar.titleTextAttributes = dict as? [NSAttributedString.Key : Any]
        // 设置导航栏默认返回按钮
        self.leftButtonActionWithImg("icon_default_back")
        // 设置导航栏背景色
        self.setBarTintColor("FFFFFF")
        
//        self.navigationController?.navigationBar.topItem?.title = "";
        
    }
    // 展示状态弹框
    func showLoadHUD(title : NSString) -> Void {
        var msg = title
            
        if msg.isEqual(to: "") {
            msg = "读取中..."
        }
        self._loadView = UIHelper.initLoadView(self, withMessage: msg as String)
        self.view.addSubview(self._loadView!)
    }
    // 清除状态弹框
    func dismissLoadHUD() -> Void {
        self._loadView?.removeFromSuperview()
    }
        
    // cocoa --
    // 设置导航栏背景透明
    public func navigationBarClear() {
        //设置导航栏背景透明
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(),
                                                                    for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    // 设置默认导航栏背景
    public func navigationBarDefault() {
        self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
        self.navigationController?.navigationBar.shadowImage = nil
    }
    // 设置默认导航栏背景色
    func setBarTintColor(_ colorString: String){
        self.navigationController?.navigationBar.barTintColor = UIColor.init("colorString") ?? UIColor.white
    }
    
    // 导航栏左边按钮
    func leftButtonActionWithImg(_ image: String) {
        let naviLeftButton = UIButton.init(type: UIButton.ButtonType.custom)
        naviLeftButton.frame = CGRect.init(x: 0, y: 0, width: 22, height: 22);
        naviLeftButton.setBackgroundImage(UIImage.init(named: image), for: UIControl.State.normal)
        naviLeftButton.setBackgroundImage(UIImage.init(named: image), for: UIControl.State.highlighted)
        naviLeftButton.addTarget(self, action: #selector(doLeftNaviAction), for: UIControl.Event.touchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: naviLeftButton)
    }
    
    //导航栏右边按钮
    func rightButtonActionWithImg(_ image: String) {
        let naviRightButton = UIButton.init(type: UIButton.ButtonType.custom)
        naviRightButton.frame = CGRect.init(x: 0, y: 0, width: 22, height: 22);
        naviRightButton.setBackgroundImage(UIImage.init(named: image), for: UIControl.State.normal)
        naviRightButton.setBackgroundImage(UIImage.init(named: image), for: UIControl.State.highlighted)
        naviRightButton.addTarget(self, action: #selector(doRightNaviAction), for: UIControl.Event.touchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: naviRightButton)
    }
    
    // 导航栏左边按钮点击
    @objc func doLeftNaviAction() {
        self.back()
    }
    
    // 导航栏右边按钮点击
    @objc func doRightNaviAction() {
        
    }
    
    // 返回
    func back() {
        self.navigationController?.popViewController(animated: true)
    }
    
    deinit {
        print(NSStringFromClass(BaseViewController.self),"这损色已释放")
    }
    
}
